import { Component, Input, OnInit, AfterViewChecked } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { QuestionControlService } from './question-control.service';

import { QuestionBase } from './question-base';

@Component({
    selector: 'guide-dynamic-form',
    templateUrl: './dynamic-form.component.html',
    providers: [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {
    @Input() questions: QuestionBase<any>[] = [];
    form: FormGroup;
    payLoad = '';
    // ngAfterViewChecked() {
    //     // this.onValueChanged();
    // }
    constructor(private qcs: QuestionControlService) { }
    ngOnInit() {
        this.form = this.qcs.toFormGroup(this.questions);
        // this.form.valueChanges
        //     .subscribe(data => this.onValueChanged(data));
        // this.onValueChanged(); // (re)set validation messages now
    }
    // onValueChanged(data?: any) {
    //     if (!this.form) { return; }
    //     const form = this.form;
    //     for (const key in form.controls) {
    //         if (form.controls.hasOwnProperty(key)) {
    //             const control = form.controls[key];
    //             if (control.errors) {
    //                 for (const error in control.errors) {
    //                     if (control.errors.hasOwnProperty(error)) {

    //                     }
    //                 }
    //             }
    //         }
    //     }
    // }
    onSubmit() {
        this.payLoad = JSON.stringify(this.form.value);
    }
}
